# 将文件夹中的所有 Excel 合并成一个

from openpyxl import load_workbook, Workbook
import os


def copy_data():
    wb = Workbook()
    sh = wb.active
    all_data = []    # 用来存放所有数据

    path = "../02.py_excelFile/销售单"
    dirs = os.listdir(path)

    # 输出所有文件和文件夹
    for file in dirs:
        filePath = path + '/' + file
        tmp_wb = load_workbook(filePath)
        tmp_sh = tmp_wb.active
        for r in range(1, tmp_sh.max_row + 1):
            # 汇总整行数据
            row_value = []
            for c in range(1, tmp_sh.max_column):
                value = tmp_sh.cell(r, c).value
                row_value.append(value)
            # 将整行数据，汇总到全局数据中
            if row_value not in all_data:       # 去重操作
                all_data.append(row_value)
    for data in all_data:
        sh.append(data)

    wb.save('../02.py_excelFile/合并的数据.xlsx')


if __name__ == '__main__':
    copy_data()






